import 'package:flutter/material.dart';
import 'dart:async';

class MyDialog extends Dialog {
  final String title;
  final String content;
  final int duration;

  MyDialog({
    @required this.title,
    @required this.content,
    this.duration
  });

  void _showTimer(context) {
    Timer.periodic(
      Duration(milliseconds: duration), // 三秒后关闭
      (t) {
        Navigator.of(context).pop();
        t.cancel(); // 取消定时器
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    if (this.duration!=null) _showTimer(context);
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          width: 150,
          height: 150,
          color: Colors.white,
          child: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
                child: Stack(
                  children: <Widget>[
                    Align(
                      child: Text('$title'),
                      alignment: Alignment.center,
                    ),
                    Align(
                      child: InkWell(
                        child: Icon(Icons.close),
                        onTap: () {
                          Navigator.of(context).pop();
                        },
                      ),
                      alignment: Alignment.topRight,
                    )
                  ],
                ),
              ),
              Divider(),
              Container(
                padding: EdgeInsets.fromLTRB(10, 0, 10, 10),
                width: double.infinity, // 宽度等于外部容器的宽度
                child: Text('$content'),
              )
            ],
          ),
        ),
      ),
    );
  }
}
